import wx
import wx.adv
import sys
import threading
import os
import socket
import time
import base64
from io import BytesIO
import random

import images


import setting
import hook_socket
import qt_window


class MyFramelogin(wx.Frame):
    def __init__(self,parent,sock_recv,sock_send,web_sock_recv,web_sock_send):
        wx.Frame.__init__(self,parent,size=wx.Size(500,300))
        self.Centre(wx.BOTH)
        self.sock_send = sock_send
        self.sock_recv = sock_recv
        self.web_sock_recv = web_sock_recv
        self.web_sock_send = web_sock_send
        self.button = wx.Button(self, label=u"登录", pos=(170,50),size=(150, 120))
        self.ip_text = wx.TextCtrl(self, value=setting.MYCONFIG.f_ip,pos=(100,10),size=(300, 24))
        self.choice1 = wx.Choice(self, pos=(30,100), size=wx.Size(80,24), choices=['本机登录','匿名登录'])
        self.choice1.SetSelection(0)
        self.button.Bind(wx.EVT_BUTTON, self.login)
        self.Bind(wx.EVT_CLOSE, self.on_close)
        mywxsock(self,self.web_sock_recv).start()

    def on_close(self, event):
        self.sock_send.put({'type':'close'})
        self.web_sock_recv.put({'type':'close'})
        wx.Exit()
        sys.exit()
        self.Destroy()

    def login(self,egve):
        iptxt = self.ip_text.GetValue()
        if self.choice1.GetSelection() == 1:
            setting.MYCONFIG.ffffffffffffffff = ''.join([str(random.randint(0,9)) for i in range(0,7)])
        self.sock_send.put({'type':'next','msg':iptxt,'id':setting.MYCONFIG.ffffffffffffffff})
        self.button.Enable(False)

    def on_error(self):
        self.button.SetLabel("登录失败")
        self.button.Enable(True)

    def on_show(self):
        self.sock_send.put({'type':'login'})
        iptxt = self.ip_text.GetValue()
        myfr = MyFrameMain(None,self.web_sock_send,self.sock_send,iptxt)
        myfr.Show(True)
        mywxsock(myfr,self.web_sock_recv).start()
        mysockrere(myfr,self.sock_recv).start()
        self.Destroy()


class FolderBookmarkTaskBarIcon(wx.adv.TaskBarIcon):
    TITLE = '群控'

    MENU_ID1, MENU_ID2 = wx.NewIdRef(count=2)

    def __init__(self,windows):
        super().__init__()
        by = BytesIO(base64.b64decode(images.imge))
        p = wx.Image(by, type=wx.BITMAP_TYPE_ICO)
        by.close()
        wxico = wx.Icon()
        wxico.CopyFromBitmap(wx.Bitmap(p))
        self.SetIcon(wxico, self.TITLE)
        self.Bind(wx.EVT_MENU, self.myshow, id=self.MENU_ID1)
        self.Bind(wx.EVT_MENU, self.myexit, id=self.MENU_ID2)
        self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.showset)
        self.windows = windows

    def CreatePopupMenu(self):
        menu = wx.Menu()
        menu.Append(self.MENU_ID1, '打开')
        menu.Append(self.MENU_ID2, '退出')
        return menu

    def myshow(self, event):
        self.windows.Show(True)
        self.windows.ToggleWindowStyle(wx.STAY_ON_TOP)
        self.windows.ToggleWindowStyle(wx.STAY_ON_TOP)

    def myexit(self, event):
        exit = False
        if event:
            ret = wx.MessageBox("确定退出吗？", "提示",wx.YES_NO, None)
            if ret == wx.YES:
                exit = True
        else:
            exit = True
        if exit == False:
            return 
        setting.MYCONFIG.set_cfg("f_ip",self.windows.myset.f_TextCtrl1.GetValue())
        setting.MYCONFIG.set_cfg("f_is",self.windows.myset.f_choice1.GetSelection())
        setting.MYCONFIG.set_cfg("k_tu",self.windows.myset.k_choice2.GetSelection())
        setting.MYCONFIG.set_cfg("k_port",self.windows.myset.k_TextCtrl1.GetValue())
        setting.MYCONFIG.set_cfg("k_pos",self.windows.myset.k_choice1.GetSelection())
        setting.MYCONFIG.set_cfg("k_img",self.windows.myset.k_choice3.GetSelection())
        self.windows.web_sock_send.put({'type':'close'})
        self.windows.sock_send.put({'type':'close'})
        try:
            for usr in self.windows.app:
                self.windows.app[usr].close()
        except Exception as e:
            print(e)
        self.windows.Destroy()
        self.Destroy()
        hook_socket.isrun(True)
        wx.Exit() 
        sys.exit()

    def showset(self,eve):
        self.windows.Maximize(False)
        self.windows.Show(True)
        self.windows.ToggleWindowStyle(wx.STAY_ON_TOP)
        self.windows.ToggleWindowStyle(wx.STAY_ON_TOP)


class MyFrameSet(object):
    def __init__(self,parent,iping):
        self.windows = parent
        font10 = wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL, False, '华文行楷')
        font12 = wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL, False, '华文行楷')
        font15 = wx.Font(15, wx.MODERN, wx.NORMAL, wx.NORMAL, False, '华文行楷')
        wx.StaticLine(self.windows, pos=(6,50), size=(220,2),style=wx.LI_HORIZONTAL)
        wx.StaticLine(self.windows, pos=(5,51), size=(2,360),style=wx.LI_VERTICAL)
        self.f_Button = wx.Button(self.windows, label=u"服务器设置", pos=(0,0),size=(115, 36))
        self.k_Button = wx.Button(self.windows, label=u"客户端设置", pos=(115,0),size=(115, 36))
        self.k_StaticText1 = wx.StaticText(self.windows, label=u"分辨率", pos=(10,130),size=wx.Size(70, 24),style=wx.ALIGN_CENTRE_HORIZONTAL)
        self.k_choice1 = wx.Choice(self.windows,  pos=(100,130), size=wx.Size(120,24), choices=setting.RESOLUTION)
        self.k_StaticText2 = wx.StaticText(self.windows, label=u"连接方式", pos=(10,70),size=wx.Size(70, 24),style=wx.ALIGN_CENTRE_HORIZONTAL)
        self.k_choice2 = wx.Choice(self.windows,  pos=(100,70), size=wx.Size(120,24), choices=setting.LINK)
        self.k_StaticText3 = wx.StaticText(self.windows, label=u"图像压缩", pos=(10,160),size=wx.Size(70, 24),style=wx.ALIGN_CENTRE_HORIZONTAL)
        self.k_choice3 = wx.Choice(self.windows,  pos=(100,160), size=wx.Size(120,24), choices=setting.IMAGE)
        self.f_StaticText1 = wx.StaticText(self.windows, label=u"地址", pos=(10,70),size=wx.Size(70, 24),style=wx.ALIGN_CENTRE_HORIZONTAL)
        self.f_TextCtrl1 = wx.TextCtrl(self.windows, value=iping, pos=(100,70),size=wx.Size(120,24))
        self.f_StaticText2 = wx.StaticText(self.windows, label=u"信息上报", pos=(10,100),size=wx.Size(70, 24),style=wx.ALIGN_CENTRE_HORIZONTAL)
        self.f_choice1 = wx.Choice(self.windows,  pos=(100,100), size=wx.Size(120,24), choices=['是','否'])
        self.k_StaticText4 = wx.StaticText(self.windows, label=u"连接端口", pos=(10,100),size=wx.Size(70, 24),style=wx.ALIGN_CENTRE_HORIZONTAL)
        self.k_TextCtrl1 = wx.TextCtrl(self.windows, value=setting.MYCONFIG.k_port, pos=(100,100),size=wx.Size(120,24))
        self.wxlog = wx.richtext.RichTextCtrl(self.windows, value="", pos=(10,150),size=(215,250))

        self.k_choice1.SetSelection(setting.MYCONFIG.k_pos)
        self.k_choice2.SetSelection(setting.MYCONFIG.k_tu)
        self.k_choice3.SetSelection(setting.MYCONFIG.k_img)
        self.f_choice1.SetSelection(setting.MYCONFIG.f_is)
        
        self.k_StaticText4.SetFont(font12)
        self.f_StaticText2.SetFont(font12)
        self.f_StaticText1.SetFont(font12)
        self.k_StaticText3.SetFont(font12)
        self.k_StaticText2.SetFont(font12)
        self.k_StaticText1.SetFont(font12)
        self.f_Button.SetFont(font15)
        self.k_Button.SetFont(font15)
        self.f_choice1.Bind(wx.EVT_CHOICE, self.on_selete)
        self.k_Button.Bind(wx.EVT_BUTTON, lambda evt, mark=1 : self.on_Button(evt,mark))
        self.f_Button.Bind(wx.EVT_BUTTON, lambda evt, mark=2 : self.on_Button(evt,mark))
        self.k_Button.Enable(False)
        self.f_StaticText1.Show(False)
        self.f_StaticText2.Show(False)
        self.f_TextCtrl1.Show(False)
        self.f_choice1.Show(False)
        self.wxlog.Show(False)

        self.wxlog.SetStyle(0,20,wx.TextAttr("white","black"))
        self.wxlog.SetDefaultStyle(wx.TextAttr("red","green"))

    def on_Button(self,eve,but):
        if but == 1:
            self.k_Button.Enable(False)
            self.f_StaticText1.Show(False)
            self.f_StaticText2.Show(False)
            self.f_choice1.Show(False)
            self.f_TextCtrl1.Show(False)
            self.wxlog.Show(False)
            self.f_Button.Enable(True)
            self.k_choice1.Show(True)
            self.k_choice2.Show(True)
            self.k_choice3.Show(True)
            self.k_StaticText1.Show(True)
            self.k_StaticText2.Show(True)
            self.k_StaticText3.Show(True)
            self.k_StaticText4.Show(True)
            self.k_TextCtrl1.Show(True)
        elif but == 2:
            self.k_Button.Enable(True)
            self.f_StaticText1.Show(True)
            self.f_StaticText2.Show(True)
            self.f_choice1.Show(True)
            self.f_TextCtrl1.Show(True)
            self.on_selete(None)
            self.f_Button.Enable(False)
            self.k_choice1.Show(False)
            self.k_choice2.Show(False)
            self.k_choice3.Show(False)
            self.k_StaticText1.Show(False)
            self.k_StaticText2.Show(False)
            self.k_StaticText3.Show(False)
            self.k_StaticText4.Show(False)
            self.k_TextCtrl1.Show(False)

    def on_selete(self,eve):
        if self.f_choice1.GetSelection() == 1:
            self.wxlog.Show(False)
        else:
            self.wxlog.Show(True)
            self.wxlog.Clear()


class MyFrameQQ(wx.Frame):
    def __init__(self,p):
        wx.Frame.__init__(self,p,size=wx.Size(300,350),style=wx.STAY_ON_TOP)
        font15 = wx.Font(15, wx.MODERN, wx.NORMAL, wx.NORMAL, False, '华文行楷')
        wx.adv.HyperlinkCtrl(self, label=u"加群反馈", url=u"https://jq.qq.com/?_wv=1027&k=iLa9ovUE",pos=(110,10),size=wx.Size(80, 24)).SetFont(font15)
        by = BytesIO(base64.b64decode(images.image))
        g = wx.Image(by, type=wx.BITMAP_TYPE_PNG)
        by.close()
        g=g.Scale(200,200)
        
        wx.StaticBitmap(self,bitmap=wx.Bitmap(g),pos=(50,50))
        wx.StaticText(self, label=u"支付宝捐赠", pos=(95,270),size=wx.Size(60, 24)).SetFont(font15)


class MyFrameMain(wx.Frame):
    def __init__(self,parent,web_sock_send,sock_send,iping):
        wx.Frame.__init__(self, parent, size=wx.Size(950,530), style=wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER|wx.MAXIMIZE_BOX))
        self.web_sock_send = web_sock_send
        self.sock_send = sock_send
        self.app = {}
        self.dictm = {}
        self.Centre(wx.BOTH)
        self.mypan1 = wx.Panel(self,pos=(5,5),size=wx.Size(935,70))
        self.mypan2 = wx.Panel(self,pos=(5,80),size=wx.Size(935,415))
        self.mypan3 = wx.Panel(self,pos=(710,80),size=wx.Size(230,415))
        font10 = wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL, False, '华文行楷')
        font12 = wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL, False, '华文行楷')
        font15 = wx.Font(15, wx.MODERN, wx.NORMAL, wx.NORMAL, False, '华文行楷')
        wx.StaticLine(self.mypan1, pos=(300,2), size=(2,68),style=wx.LI_VERTICAL)
        wx.StaticLine(self.mypan1, pos=(470,2), size=(2,68),style=wx.LI_VERTICAL)
        wx.StaticText(self.mypan1, label=u"内置指令", pos=(10,15),size=wx.Size(60, 24),style=wx.ALIGN_CENTRE_HORIZONTAL).SetFont(font10)
        wx.StaticText(self.mypan1, label=u"CMD命令", pos=(10,45),size=wx.Size(60, 24),style=wx.ALIGN_CENTRE_HORIZONTAL).SetFont(font10)
        self.scroller = wx.ScrolledWindow(self.mypan2,size=wx.Size(935,415))
        self.scroller.SetScrollRate(0,20)
        self.width, self.height = 10,10
        self.scroller.SetVirtualSize((935,self.height+10))

        self.p_choice1 = wx.Choice(self.mypan1,  pos=(80,10), size=wx.Size(150,22), choices=setting.CMD_LIST)
        self.cmd_TextCtrl = wx.TextCtrl(self.mypan1, value=u"cmd", pos=(80,40),size=wx.Size(150,24))
        self.vialn_button = wx.Button(self.mypan1, label=u"发送", pos=(240,10),size=(50, 24))
        self.cmd_button = wx.Button(self.mypan1, label=u"执行", pos=(240,40),size=(50, 24))
        self.update_button = wx.Button(self.mypan1, label=u"上传包", pos=(310,5),size=(150, 30))
        self.search_button = wx.Button(self.mypan1, label=u"搜索", pos=(610,38),size=(50, 30))
        self.search_TextCtrl = wx.TextCtrl(self.mypan1, value=u"", pos=(480,40),size=wx.Size(130,26),style=wx.TE_PROCESS_ENTER)
        self.update_TextCtrl = wx.TextCtrl(self.mypan1, value=u"", pos=(310,40),size=wx.Size(150,26))
        self.xz_checkBox = wx.CheckBox(self.mypan1,label="全选", pos=(480,5),size=wx.Size(70,20))
        self.set_button = wx.Button(self.mypan1, label=u"打开设置", pos=(820,5),size=(100, 30))
        self.donation_button = wx.Button(self.mypan1, label=u"加群反馈", pos=(820,38),size=(100, 30))
        self.update_button.SetFont(font12)
        self.cmd_button.SetFont(font12)
        self.vialn_button.SetFont(font12)
        self.p_choice1.SetSelection(0)

        self.vialn_button.Bind(wx.EVT_BUTTON, lambda evt, mark=1 : self.on_Button(evt,mark))
        self.update_button.Bind(wx.EVT_BUTTON, lambda evt, mark=2 : self.on_Button(evt,mark))
        self.cmd_button.Bind(wx.EVT_BUTTON, lambda evt, mark=3 : self.on_Button(evt,mark))
        #self.donation_button.Bind(wx.EVT_BUTTON, lambda evt, mark=1 : self.on_selete(evt,mark))
        self.set_button.Bind(wx.EVT_BUTTON, lambda evt, mark=2 : self.on_selete(evt,mark))
        self.xz_checkBox.Bind(wx.EVT_CHECKBOX, lambda evt, mark=3 : self.on_selete(evt,mark))
        self.search_button.Bind(wx.EVT_BUTTON, self.on_search)
        self.search_TextCtrl.Bind(wx.EVT_TEXT_ENTER, self.on_search)
        self.myset = MyFrameSet(self.mypan3,iping)
        self.mypan3.Show(False)
        self.pmenu = wx.Menu()
        item = self.pmenu.Append(1, "禁用")
        self.Bind(wx.EVT_MENU, self.on_PopupItemSelected, item)
        mydir = FileDrop(self.update_TextCtrl)
        self.update_TextCtrl.SetDropTarget(mydir)
        self.Baricon = FolderBookmarkTaskBarIcon(self)
        self.Bind(wx.EVT_CLOSE, self.on_cloase)

        self.donation_button.Bind(wx.EVT_MOTION, lambda evt, mark=4 : self.on_selete(evt,mark))
        self.Bind(wx.EVT_MOTION, lambda evt, mark=5 : self.on_selete(evt,mark))
        #self.Bind(wx.EVT_MOUSE_CAPTURE_CHANGED, lambda evt, mark=5 : self.on_selete(evt,mark))
        #self.Bind(wx.EVT_MOUSE_CAPTURE_LOST, lambda evt, mark=4 : self.on_selete(evt,mark))
        #self.donation_button.Bind(wx.EVT_MOUSE_CAPTURE_CHANGED, lambda evt, mark=5 : self.on_selete(evt,mark))
        self.QQframe = MyFrameQQ(self)

    def on_selete(self,eve,mak):
        if mak == 1:
            self.cmd_TextCtrl.SetValue(setting.CMD_CMD[self.p_choice1.GetSelection()])
        elif mak == 2:
            if self.set_button.GetLabel() == "打开设置":
                self.mypan3.Show(True)
                self.mypan2.SetSize(700,415)
                self.set_button.SetLabel("关闭设置")
            else:
                self.mypan3.Show(False)
                self.mypan2.SetSize(935,415)
                self.set_button.SetLabel("打开设置")
        elif mak == 3:
            if self.xz_checkBox.GetValue():
                for i in self.dictm:
                    if self.xz_checkBox.GetLabelText() == "搜索选中":
                        if self.dictm[i]['checkBox'].GetBackgroundColour().GetRGB() == 65280:
                            if self.dictm[i]['ist']:
                                self.dictm[i]['checkBox'].SetValue(True)
                    else:
                        if self.dictm[i]['ist']:
                            self.dictm[i]['checkBox'].SetValue(True)
            else:
                for i in self.dictm:
                    if self.dictm[i]['checkBox'].GetValue():
                        self.dictm[i]['checkBox'].SetValue(False)
        elif mak == 4:
            self.QQframe.Show(True)
            x,y = self.GetPosition()
            self.QQframe.Move(x+645,y+100)
        elif mak == 5:
            self.QQframe.Show(False)
            #print(self.GetPosition())
            #self.QQframe.Maximize(False)
            #self.QQframe.Move(self.GetPosition())

    def on_search(self,eve):
        #scrollrange = self.scroller.GetScrollRange(wx.VERTICAL)
        search = self.search_TextCtrl.GetValue()
        wxcol = wx.Colour()
        wxcol.SetRGB(15790320)
        ransum = self.scroller.GetScrollPos(wx.VERTICAL)
        if search:
            sss = False
            for i in self.dictm:
                if search in self.dictm[i]['checkBox'].GetLabelText():
                    self.dictm[i]['checkBox'].SetBackgroundColour("green")
                    if not sss:
                        self.xz_checkBox.SetLabelText("搜索选中")
                        self.scroller.Scroll(0,self.dictm[i]['checkBox'].GetPosition()[1]//20)
                        sss = True
                elif self.dictm[i]['checkBox'].GetBackgroundColour().GetRGB() != 15790320:
                    self.dictm[i]['checkBox'].SetBackgroundColour(wxcol)
        else:
            self.xz_checkBox.SetLabelText("全选")
            for i in self.dictm:
                if self.dictm[i]['checkBox'].GetBackgroundColour().GetRGB() != 15790320:
                    self.dictm[i]['checkBox'].SetBackgroundColour(wxcol)
        self.scroller.Refresh(True)

    def on_Button(self,eve,but):
        _app = []
        for i in self.dictm:
            if self.dictm[i]['checkBox'].GetValue():
                _app.append(i)
        if not _app:
            wx.MessageBox("你需要勾选用户", "提示",wx.OK, self)
            return 
        if but == 1:
            self.web_sock_send.put({'type':'send','data':{'type':'inlay','report':True,'msg':setting.CMD_CMD[self.p_choice1.GetSelection()],'list':_app}})
            wx.MessageBox("指令已发送", "通知",wx.OK, self)
        elif but == 2:
            p = self.update_TextCtrl.GetValue()
            if not p:
                wx.MessageBox("你需要把文件拖到上传包下方", "提示",wx.OK, self)
                return 
            if os.path.isfile(os.path.abspath(p)):
                ip = socket.gethostbyname(socket.gethostname())
                name = os.path.basename(os.path.abspath(p))
                size = os.path.getsize(os.path.abspath(p))
                for i in _app:
                    port = hook_socket.ret_prot()
                    self.web_sock_send.put({'type':'send','data':{'type':'update','report':False,'msg':{'name':name, 'size':size,'ip':(ip,port)},'list':[i]}})
                    qt_window.progress("update",os.path.abspath(p),(ip,port),i).show()
            else:
                wx.MessageBox("找不到文件：" + p, "提示",wx.OK, self)
        elif but == 3:
            cmd = self.cmd_TextCtrl.GetValue()
            self.web_sock_send.put({'type':'send','data':{'type':'cmd','report':True,'msg':cmd,'list':_app}})
            wx.MessageBox("指令已发送", "通知",wx.OK, self)

    def on_checkBox_click(self,eve,id):
        if not self.dictm[id]['ist']:
            self.dictm[id]['checkBox'].SetValue(False)

    def on_PopupItemSelected(self,eve):
        id = self.pmenu.GetHelpString(1)
        if self.dictm[id]['ist']:
            self.dictm[id]['ist'] = False
            self.dictm[id]['checkBox'].SetValue(False)
            self.dictm[id]['button'].SetLabel("查看")
        else:
            self.dictm[id]['ist'] = True
            self.dictm[id]['button'].SetLabel("控制")

    def on_ShowPopup(self, event,id):
        if self.dictm[id]['ist']:
            self.pmenu.SetLabel(1,"禁用")
        else:
            self.pmenu.SetLabel(1,"启用")
        pos = event.GetPosition()
        pos = self.ScreenToClient(pos)
        self.pmenu.SetHelpString(1,id)
        self.PopupMenu(self.pmenu, pos)

    def on_mybutton(self,eve,id):
        connt = setting.LINK[self.myset.k_choice2.GetSelection()]
        pos_xy = setting.SHARPNESS[self.myset.k_choice1.GetSelection()]
        quite = setting.PICTURE[self.myset.k_choice3.GetSelection()]
        port = self.myset.k_TextCtrl1.GetValue()
        self.Show(False)
        self.web_sock_send.put({'type':'send','data':{'type':connt,'report':True,'msg':{'time':"2",'quality':quite,"x":pos_xy[0],"y":pos_xy[1],"port":int(port),'ip':socket.gethostbyname(socket.gethostname())},'list':[id]}})

    def on_cloase(self,eve):
        self.Show(False)

    def my_message_exit(self,n,msg=None):
        if n:
            wx.MessageBox(msg, "提示",wx.OK, self)
            self.Baricon.myexit(None)
        else:
            wx.MessageBox(msg, "错误提示",wx.OK, self)
            self.Show(True)

    def my_add_app(self,id):
        if id[0] in self.dictm:
            self.dictm[id[0]]['checkBox'].Enable(True)
            self.dictm[id[0]]['button'].Enable(True)
            self.dictm[id[0]]['checkBox'].SetLabel(id[0]+ "  " +id[1])
        else:
            ransum = self.scroller.GetScrollPos(wx.VERTICAL)
            m_checkBox = wx.CheckBox(self.scroller,label=id[0]+ "  " +id[1], pos=(self.width, self.height-(ransum*20)),size=(240,24))
            m_button = wx.Button(self.scroller, label=u"控制",pos=(self.width+250, self.height-(ransum*20)),size=(35, 20))
            m_button.Bind(wx.EVT_BUTTON, lambda evt, mark=id[0] : self.on_mybutton(evt,mark))
            m_checkBox.Bind(wx.EVT_CONTEXT_MENU, lambda evt, mark=id[0] : self.on_ShowPopup(evt,mark))
            m_checkBox.Bind(wx.EVT_CHECKBOX, lambda evt, mark=id[0] : self.on_checkBox_click(evt,mark))
            self.dictm.update({id[0]:{'checkBox':m_checkBox,'button':m_button,'ist':True}})
            if self.width+300 >=900:
                self.width=10
                self.height += 30
            else:
                self.width+=300
            self.scroller.SetVirtualSize((935,self.height+10))

    def my_show(self,n):
        self.Show(True)
        self.Maximize(False)
        self.ToggleWindowStyle(wx.STAY_ON_TOP)
        self.ToggleWindowStyle(wx.STAY_ON_TOP)

    def my_create(self,id,ip,posx,posy,typ):
        if self.dictm[id]['ist']:
            self.sock_send.put({'type':'add','app':id})
            demo = qt_window.Demo_Comm(None,title=id,IP=ip,pos_xy=(posx,posy,typ),sok=self.web_sock_send,soc=self.sock_send,per=self)
            demo.show()
            self.app.update({id:demo})
        else:
            demo = qt_window.Demo_See(None,title=id,IP=ip,pos_xy=(posx,posy,typ),per=self)
            demo.show()

    def my_set_del_app(self,id,n):
        if n:
            self.dictm[id]['checkBox'].Enable(False)
            self.dictm[id]['button'].Enable(False)
        else:
            self.dictm[id[0]]['checkBox'].SetLabel(id[0]+ "  " +id[1])

    def my_logtxt(self,txt):
        if self.myset.f_choice1.GetSelection() == 0:
            self.myset.wxlog.AddParagraph(time.strftime("%Y-%m-%d %H:%M:%S") + " : " + txt)
            self.myset.wxlog.AppendText(":")
            self.myset.wxlog.ShowPosition(-1)

    def my_update(self,dic):
        qt_window.progress("getdate",(dic['size'],dic['name']),(dic['ip'][0],dic['ip'][1]),dic['name']).show()


class FileDrop(wx.FileDropTarget):
    def __init__(self,windows):
        wx.FileDropTarget.__init__(self)
        self.windows = windows

    def OnDropFiles(self, x, y, filePath):
        if filePath:
            for i in filePath:
                self.windows.SetValue(i)
            return True
        else:
            return False


class mywxsock(threading.Thread):
    def __init__(self,windows,web_sock_recv):
        threading.Thread.__init__(self)
        self.windows = windows
        self.web_sock_recv = web_sock_recv

    def run(self):
        while True:
            msg = self.web_sock_recv.get(True)
            if msg['type'] == 'tcp':
                wx.CallAfter(self.windows.my_create,msg['msg']['model'],(msg['msg']['ip'],msg['msg']['port']),msg['msg']['pos_x'],msg['msg']['pos_y'],msg['type'])
            elif msg['type'] == 'udp':
                wx.CallAfter(self.windows.my_create,msg['msg']['model'],(msg['msg']['ip'],msg['msg']['port']),msg['msg']['pos_x'],msg['msg']['pos_y'],msg['type'])
            elif msg['type'] == 'del':
                wx.CallAfter(self.windows.my_set_del_app,msg['msg'],True)
            elif msg['type'] == 'set':
                wx.CallAfter(self.windows.my_set_del_app,msg['msg'],False)
            elif msg['type'] == 'add':
                wx.CallAfter(self.windows.my_add_app,msg['msg'])
            elif msg['type'] == 'get-list':
                for use in msg['msg']:
                    wx.CallAfter(self.windows.my_add_app,(use,msg['msg'][use]))
            elif msg['type'] == 'update':
                wx.CallAfter(self.windows.my_update,msg['msg'])
            elif msg['type'] == 'err':
                wx.CallAfter(self.windows.my_message_exit,False,msg['msg'])
            elif msg['type'] == 'ret':
                wx.CallAfter(self.windows.my_logtxt,msg['msg'])
            elif msg['type'] == 'isrun':
                wx.CallAfter(self.windows.my_show,msg['msg'])
            elif msg['type'] == 'close':
                print('mywxsock: exit')
                break
            elif msg['type'] == 'webclose':
                wx.CallAfter(self.windows.my_message_exit,True,"服务器关闭了")
            elif msg['type'] == 'logon':
                if msg['msg'] == 'successful':
                    wx.CallAfter(self.windows.on_show)
                    break
                elif msg['msg'] == 'failure':
                    wx.CallAfter(self.windows.on_error)
            elif msg['type'] == 'exit':
                if msg['msg'] == 'pywebsocket':
                    wx.CallAfter(self.windows.my_message_exit,True,"已与服务器断开连接")



class mysockrere(threading.Thread):
    def __init__(self,windows,sock_recv):
        threading.Thread.__init__(self)
        self.windows = windows
        self.sock_recv = sock_recv

    def run(self):
        while True:
            msg = self.sock_recv.get(True)
            if msg['type'] == 'key_down':
                self.windows.app[msg['app']].thread.on_send(hook_socket.return_send(typ='key_down',flag=msg['key']))
            elif msg['type'] == 'key_up':
                self.windows.app[msg['app']].thread.on_send(hook_socket.return_send(typ='key_up',flag=msg['key']))
            elif msg['type'] == 'close':
                print('mysockrere: exit')
                break